﻿using LCL.LData;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace LCL.CodeHelper {
    public partial class CodeCommon {
        public static List<ColumnInfo> GetColumnInfos(DataTable dt) {
            List<ColumnInfo> keys = new List<ColumnInfo>();
            if(dt != null) {
                ArrayList colexist = new ArrayList();//是否已经存在
                ColumnInfo key;
                foreach(DataRow row in dt.Rows) {
                    string Colorder = row["Colorder"].ToString();
                    string ColumnName = row["ColumnName"].ToString();
                    string ColumnType = row["ColumnType"].ToString();
                    string isIdentity = row["IsIdentity"].ToString();
                    string IsPK = row["PK"].ToString();
                    string Length = row["MaxLength"].ToString();
                    string cisNull = row["Isnullable"].ToString();
                    string DefaultVal = row["DefaultVal"].ToString();
                    string DeText = row["DeText"].ToString();
                    key = new ColumnInfo();
                    key.ColumnOrder = Colorder;
                    key.ColumnName = ColumnName;
                    key.TypeName = ColumnType;
                    key.IsIdentity = (isIdentity == "√") ? true : false;
                    key.IsPrimaryKey = (IsPK == "√") ? true : false;
                    key.Length = Length;
                    key.Nullable = (cisNull == "√") ? true : false;
                    key.DefaultVal = DefaultVal;
                    key.Description = DeText;
                    if(!colexist.Contains(ColumnName)) {
                        keys.Add(key);
                        colexist.Add(ColumnName);
                    }
                }
                return keys;
            } else {
                return null;
            }

        }
        public static DataTable GetTableColumnDt(List<ColumnInfo> collist) {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("ColumnName");
            dataTable.Columns.Add("ColumnType");
            dataTable.Columns.Add("MaxLength");
            dataTable.Columns.Add("Identifying");
            dataTable.Columns.Add("PK");
            dataTable.Columns.Add("Isnullable");
            dataTable.Columns.Add("DefaultValue");
            dataTable.Columns.Add("ColumnRemark");
            foreach(ColumnInfo current in collist) {
                DataRow dataRow = dataTable.NewRow();
                dataRow["ColumnName"] = current.ColumnName;
                dataRow["ColumnType"] = current.TypeName;
                dataRow["MaxLength"] = current.Length;
                dataRow["Identifying"] = (current.IsIdentity ? "√" : "");
                dataRow["PK"] = (current.IsPrimaryKey ? "√" : "");
                dataRow["Isnullable"] = (current.Nullable ? "√" : "");
                dataRow["DefaultValue"] = current.DefaultVal;
                dataRow["ColumnRemark"] = current.Description;
                dataTable.Rows.Add(dataRow);
            }
            return dataTable;
        }
        public static string GetClassName(string tableName,string format) {
            return NameRule.CreateFirstUpper(format).GetClassName(tableName);
        }
        //// XX_XX  XX/XX生成目录
        public static string Tablepath(string tablename) {
            // XX_XX
            string dir = tablename.Replace("_","/");
            FolderCheck(dir);
            return dir;
        }
        // xxx,xxxx,xxx,xxx
        public static string GetSeleteColumn(List<ColumnInfo> keys,string flg = "",bool IdentityisPrior = false) {
            StringPlus stringPlus = new StringPlus();
            ColumnInfo identityKey = CodeCommon.GetIdentityKey(keys);
            if(IdentityisPrior && identityKey != null) {
                stringPlus.Append(flg + identityKey.ColumnName + flg + ",");
            } else {
                foreach(ColumnInfo current in keys) {
                    if(current.IsPrimaryKey || !current.IsIdentity) {
                        stringPlus.Append(flg + current.ColumnName + flg + ",");
                    }
                }
                if(!string.IsNullOrWhiteSpace(flg)) {
                    stringPlus.DelLastChar(flg);
                }
                stringPlus.DelLastComma();
            }
            return stringPlus.Value;
        }
        //xxx=#xxx#,xxx=#xxx#,xxx=#xxx#
        public static string GetUpdateColumn(List<ColumnInfo> keys,string flg = ",",bool IdentityisPrior = false) {

            StringPlus stringPlus = new StringPlus();
            ColumnInfo identityKey = CodeCommon.GetIdentityKey(keys);
            if(IdentityisPrior && identityKey != null) {
                stringPlus.Append(identityKey.ColumnName + "=#" + identityKey.ColumnName + "#" + flg);
            } else {
                foreach(ColumnInfo current in keys) {
                    if(current.IsPrimaryKey || !current.IsIdentity) {
                        stringPlus.Append(current.ColumnName + "=#" + current.ColumnName + "#" + flg);
                    }
                }
                if(!string.IsNullOrWhiteSpace(flg)) {
                    stringPlus.DelLastChar(flg);
                }
                stringPlus.DelLastComma();
            }
            return stringPlus.Value;
        }

    }
}
